<?php
declare (strict_types = 1);

namespace hangpu8\hpcms\controller;

use hangpu8\hpcms\Utils\Json;
use thans\jwt\facade\JWTAuth;

class Base extends Baseic
{
    protected $user;

    /**
     * 构造函数
     *
     * @return void
     */
    protected function initialize()
    {
        parent::initialize();
        // 登录检测，登录后直接获取用户信息
        $this->userinfo();
    }

    /**
     * 登录检测
     *
     * @return void
     */
    private function userinfo()
    {
        try {
            $payload = JWTAuth::auth();
            $id = $payload['id']->getValue();
            $map['id'] = $id;
            $info = \hangpu8\hpcms\model\Admin::with('profile')->where($map)->find();
            if (!$info) {
                $json = Json::fail('请先登录',208)->getContent();
                echo $json;exit;
            }
            $this->user = $info->toArray();
        } catch (\Throwable $e) {
            if ($e->getMessage() == 'Must have token' && $e->getCode() == 0) {
                $json = Json::fail('您未登录或登录已超时',208)->getContent();
                echo $json;exit;
            }
        }
    }
}
